package com.shell.soda.car.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 作者: creazier.huang
 * 日期: 2017/8/25
 * 说明:
 */
@Configuration
public class VelocityConfig {
    private static Logger logger = LoggerFactory.getLogger(VelocityConfig.class);

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public  VelocityConfigurer getConf(){
        logger.info("VelocityConfigurer configure");
        VelocityConfigurer conf = new VelocityConfigurer();
        conf.setResourceLoaderPath("/WEB-INF/view/");
        Properties properties = new Properties();
        properties.put("input.encoding","UTF-8");
        properties.put("output.encoding","UTF-8");
        properties.put("directive.set.null.allowed","true");
//        properties.put("velocimacro.library","macro/macros.vm");
        properties.put("eventhandler.noescape.html.match","/(?:screen_content)|(?:noescape_.*)/");
        conf.setVelocityProperties(properties);

        return conf;

    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public VelocityLayoutViewResolver viewResolver(){
        logger.info("VelocityLayoutViewResolver configure");
        VelocityLayoutViewResolver resolver = new VelocityLayoutViewResolver();
        resolver.setContentType("text/html;charset=UTF-8");
        resolver.setToolboxConfigLocation("toolboxConfigLocation");
        resolver.setExposeRequestAttributes(true);
        resolver.setLayoutUrl("layout/default.vm");
        resolver.setOrder(1);
        resolver.setCache(true);
        resolver.setPrefix("");
        resolver.setSuffix(".vm");
        return resolver;
    }

}
